package top.wintp.juhedatademo.adapter;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import top.wintp.juhedatademo.R;
import top.wintp.juhedatademo.app.GlideApp;
import top.wintp.juhedatademo.entity.NewsInfo;

/**
 * Created by apk2sf on 2017/12/11.
 * email: apk2sf@163.com
 * QQ：337081267
 */

public class NewsListAdapter extends BaseAdapter {
    private final List<NewsInfo.ResultBean.DataBean> mDataBeans;

    public NewsListAdapter(List<NewsInfo.ResultBean.DataBean> dataBeans) {
        this.mDataBeans = dataBeans;
    }

    @Override
    public int getCount() {
        return mDataBeans.size();
    }

    @Override
    public NewsInfo.ResultBean.DataBean getItem(int position) {
        return mDataBeans.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            convertView = View.inflate(parent.getContext(), R.layout.item_news, null);

            viewHolder = new ViewHolder();
            viewHolder.ivIcon = convertView.findViewById(R.id.iv_icon);
            viewHolder.tvTitle = convertView.findViewById(R.id.tv_title);
            viewHolder.tvDesc = convertView.findViewById(R.id.tv_desc);
            viewHolder.tvAuthor = convertView.findViewById(R.id.tv_author);

            convertView.setTag(viewHolder);
        }

        viewHolder = (ViewHolder) convertView.getTag();

        NewsInfo.ResultBean.DataBean dataBean = mDataBeans.get(position);

        // 加载网络图片
        GlideApp.with(parent.getContext()).load(dataBean.getThumbnail_pic_s())
                .placeholder(R.mipmap.ic_launcher).into(viewHolder.ivIcon);

        viewHolder.tvAuthor.setText(dataBean.getAuthor_name());
        viewHolder.tvTitle.setText(dataBean.getTitle());
        viewHolder.tvDesc.setText(dataBean.getDate());




        return convertView;
    }


    static class ViewHolder {
        ImageView ivIcon;
        TextView tvTitle;
        TextView tvDesc;
        TextView tvAuthor;
    }
}
